package com.example.demo.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.entity.QuartzTask;
import com.example.demo.mapper.QuartzTaskMapper;
import com.example.demo.service.impl.QuartzServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class QuartzStartupListener implements CommandLineRunner {
    
    @Autowired
    private QuartzServiceImpl quartzService;
    
    @Autowired
    private QuartzTaskMapper quartzTaskMapper;
    
    @Override
    public void run(String... args) throws Exception {
        // 查询所有运行状态的任务
        LambdaQueryWrapper<QuartzTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QuartzTask::getTaskStatus, 1);
        List<QuartzTask> taskList = quartzTaskMapper.selectList(wrapper);
        
        // 重新调度任务
        for (QuartzTask task : taskList) {
            quartzService.addJob(task);
        }
    }
} 